//
//  shuffleTest.c
//  
//
//  Created by Preston Skaggs on 3/9/14.
/*  This tests the shuffle function in dominion.c for distribution equality
*   over varying deck sizes.  Obviously the bias in distribution will change
*   as the deck size increases but fundamentally the idea is to validate a certain
*   level of bias in the distribution to decide if the shuffle is adequate.
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dominion.h"
#include <stdbool.h>


#define NUM_TESTS 10000
#define MAX_DECK_SIZE 100

int main(){
    struct gameState *state = malloc(sizeof(struct gameState));
    int deckSize, i, j;
    
    state->numPlayers = 1;
    FILE *fd = fopen("./shuffleTestResults.out", "a");
    if(fd == NULL){
        printf("Failed to open file");
        exit(1);
    }
    for(deckSize = 10; deckSize<MAX_DECK_SIZE; deckSize++){
        fprintf(fd, "*********Start Test************\n\n");
        state->deckCount[0] = 0;        //deck count for player 0 set to zero
        
        bool lowBias = false;
        bool medBias = false;
        bool highBias = false;
        int tracker[deckSize];          //An array to hold the distribution
        state->deck[0][0] = gold;       //set first card in deck to gold so we can track it
        state->deckCount[0]++;
        tracker[0] = 0;                 // initialize first element
        for(i=1; i<deckSize; i++){
            state->deck[0][i] = copper;  //the rest of the cards are copper
            state->deckCount[0]++;
            tracker[i]=0;               //putting this here to initialize without creating another loop
        }
        for(i=0; i<NUM_TESTS; i++){
            shuffle(0, state);
            for(j=0; j<deckSize; j++){
                if(state->deck[0][j] == gold) tracker[j]++;
            }
        }
        for(i=0; i<deckSize; i++){
            double bias = fabs(1-((1.0*tracker[i])/(NUM_TESTS/deckSize)));
            if(bias<= 0.10) lowBias = true;
            if(bias> 0.10 && bias<0.25) medBias = true;
            if(bias>0.25) highBias = true;
           fprintf(fd, "Hand Postion %d: Bias %f\n", i, bias);
        }
        if(highBias){
            printf("High Bias for Hand Size %d\n", deckSize);
        }
        else if(medBias){
            printf("Medium Bias for Hand Size %d\n", deckSize);
        }
        else if(lowBias){
            printf("Low Bias for Hand Size %d\n", deckSize);
        }
        else{
            printf("Error for Hand Size %d\n", deckSize);
        }
    }
    fprintf(fd, "*********End Test************\n\n");
    return 0;
}